<?php

/**
 * Base actions for the jfPortableDevicePlugin jfPortableDevice module.
 * 
 * @package     jfPortableDevicePlugin
 * @subpackage  jfPortableDevice
 * @author      Jonas Fischer <info@jonasfischer.net>
 * @version     SVN: $Id: BaseActions.class.php 12534 2008-11-01 13:38:27Z Kris.Wallsmith $
 */
abstract class BasejfPortableDeviceActions extends sfActions
{

	public function executeRedirect()
  {
  	$request = sfContext::getInstance()->getRequest(); //symfony 1.0 compatibility
  	jfPortableDevice::setIsPortableDevice(true);
  	jfPortableDevice::setAppType('web');
  	jfPortableDevice::setDeviceSize($this->getDeviceSize());

  	$redirectUrl = $request->getParameter('redirectUrl', $this->getController()->genUrl(jfPortableDevice::getConfig('startUrl'), true));
  	$redirectUrl = jfPortableDevice::extendUrl($redirectUrl);

  	$this->redirect($redirectUrl, 302);
    throw new sfStopException();
  }
  
  protected function getDeviceSize() {
		return jfPortableDevice::DEVICE_SIZE_M;
  }
	
  public function executeAbout()
  {
  	$request = sfContext::getInstance()->getRequest(); //symfony 1.0 compatibility
  	jfPortableDevice::setConfig('useLogo', false);
  	jfPortableDevice::setConfig('useTabBar', false);
  }

  public function executeManifest()
  {
  	$request = sfContext::getInstance()->getRequest(); //symfony 1.0 compatibility
    if(method_exists($context, 'getConfiguration') && method_exists($context->getConfiguration(), 'loadHelpers')) {
      $context->getConfiguration()->loadHelpers('Url');
    }
    else if (method_exists('sfLoader', 'loadHelpers')) {
      sfLoader::loadHelpers('Url'); //maintain compatibility to symfony versions prior to 1.3
    }

  	sfConfig::set('sf_web_debug', false);
    $this->setLayout(false);
		$this->getResponse()->setContentType('text/cache-manifest');

		$this->files = array(jfPortableDevice::extendUrl(url_for(jfPortableDevice::getConfig('startUrl'))));
		
//		$this->files = $this->getCacheManifestFilesForConfig(jfPortableDevice::getConfig('includeJavascriptsForApp'), 'javascript_path');
//		$this->files = $this->getCacheManifestFilesForConfig(jfPortableDevice::getConfig('includeStylesheetsForApp'), 'stylesheet_path', $this->files);
//		$this->files = $this->getCacheManifestFilesForDirectory(sfConfig::get('sf_web_dir').jfPortableDevice::getConfig('jfPDTheme'), $this->files);
  }
  
  protected function getCacheManifestFilesForConfig($config, $callback, $files = array()) {
  	
    $context = $this->getContext();
  	// compatibility with symfony 1.0:
  	if(method_exists($context, 'getConfiguration') && method_exists($context->getConfiguration(), 'loadHelpers')) {
      $context->getConfiguration()->loadHelpers('Asset');
    }
    else if (method_exists('sfLoader', 'loadHelpers')) {
      sfLoader::loadHelpers('Asset'); //maintain compatibility to symfony versions prior to 1.3
    }
  	
  	foreach ($config as $key => $value) {
  		if (is_string($value)) {
  			$files[] = $callback($value);
  		}
  		else if (is_array($value)) {
  			foreach ($value as $filename => $options) {
  				$files[] = $callback($filename);
  			}
  		}
  	}

  	return $files;
  }
  
  protected function getCacheManifestFilesForDirectory($dir, $files = array()) {
  	$dirIterator = new RecursiveDirectoryIterator($dir);
		foreach(new RecursiveIteratorIterator($dirIterator) as $file) {
		  if ($file->IsFile() &&
		      substr($file->getFilename(), 0, 1) != "." &&
		      false === strpos((string)$file, '.svn')
		  ) {
		    $files[] = $file;
		  }
		}
		return $files;
  }

  public function executeOffline()
  {
  	$request = sfContext::getInstance()->getRequest(); //symfony 1.0 compatibility
    $this->setLayout(false);
  }

}
